package com.swizipuremvc.core
{
	
	import com.swizipuremvc.core.ApplicationModel;
	import com.swizipuremvc.core.utils.InjectionManager;
	
	import flash.utils.Dictionary;
	
	import org.puremvc.as3.multicore.interfaces.IFacade;
	import org.puremvc.as3.multicore.interfaces.IMediator;
	import org.puremvc.as3.multicore.interfaces.IProxy;
	import org.puremvc.as3.multicore.patterns.facade.Facade;
	import org.puremvc.as3.multicore.patterns.mediator.AbstractMediator;

	public class ApplicationFacade extends Facade implements IFacade
	{
		public static var key:String;

		public function ApplicationFacade()
		{
			/*key = key;
			super(key);
			serviceMap = new Array();*/
		}

		//-----------------------------------------------------------------------------------
		public static function getInstance():IFacade
		{
			//key = facadeType.toString();
			var appFacade:ApplicationFacade = new ApplicationFacade();
			appFacade.preInitialize();
			return appFacade;
		}

		public static function getFacade(key:String):ApplicationFacade
		{
			return instanceMap[key];
		}

		//-----------------------------------------------------------------------------------
		private var _injectionManager:InjectionManager;

		//-----------------------------------------------------------------------------------
		public function set injectionManager(value:InjectionManager):void
		{
			_injectionManager = value;
		}

		public function get injectionManager():InjectionManager
		{
			return _injectionManager;
		}

		//-----------------------------------------------------------------------------------
		override protected function initializeController():void
		{
			if (controller != null)
				return;
			controller = ApplicationController.getInstance(key);
		}

		override protected function initializeView():void
		{
			if (view != null)
				return;

			view = ApplicationView.getInstance(key);
		}

		override protected function initializeModel():void
		{
			if (model != null)
				return;
			model = ApplicationModel.getInstance(key);
		}
		
		public function registerService():void
		{
			
		}
		public function initialize():void
		{
			this.preInitialize();
			
			for each (var proxy:IProxy in this.proxies)
			{
				registerProxy(proxy);
			}
			for each (var mediator:IMediator in this.mediators)
			{
				(mediator as AbstractMediator).initialize(facadeType.toString(),(mediator as AbstractMediator).mediatorType.toString(),(mediator as AbstractMediator).viewComponent);
				(mediator as AbstractMediator).initializeNoteHandlers();
				registerMediator(mediator);
				
			}
			for each(var command:CommandCreator in this.commands)
			{
				registerCommand(command.commandName.toString(),command.commandClass);
			}
		}
	}
}
